package com.manzuo.framework.ibatis;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.manzuo.framework.datasource.UseWriteDataSource;

@SuppressWarnings("unchecked")
public class DaoImpl extends SqlMapClientDaoSupport implements Dao {
	
	@UseWriteDataSource
	public <T> T insert(String statementName, Object parameterObject) {
		return (T)getSqlMapClientTemplate().insert(statementName, parameterObject);
	}

	@UseWriteDataSource
	public int delete(String statementName, Object parameterObject) {
		return getSqlMapClientTemplate().delete(statementName, parameterObject);
	}

	@UseWriteDataSource
	public int update(String statementName, Object parameterObject) {
		return getSqlMapClientTemplate().update(statementName, parameterObject);
	}

	public <T> T unique(String statementName, Object parameterObject) {
		return (T)getSqlMapClientTemplate().queryForObject(statementName, parameterObject);
	}

	public <T> T uniqueLoose(String statementName, Object parameterObject) {
		List<T> list = list(statementName, parameterObject);
		if(CollectionUtils.isNotEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	public <T> List<T> list(String statementName, Object parameterObject) {
		return getSqlMapClientTemplate().queryForList(statementName, parameterObject);
	}

}
